var globalVar = {
    pageNum: 1,//分页：当前页
    pageSize: 6,//分页：每页加载内容的大小
    commPageNum: 1,//评论区分页，当前页
    commPageSize: 5,//评论区分页，每次加载条数
    noticePageNum: 1,//公告区分页
    //noticePageSize:5,//公告区分页
    noticeBtnClick: true,//用于记录公告是否继续分页
    navBarId: "",//导航条标识，用于条件查询
    //expireTime:1000*60*60*24*7,//设置本地存储过期时间,默认一个周
    expireTime: 1000 * 60 * 60 * 24 * 1,
    submitComtTem: true,//用于提交评论按钮状态的控制
    demoDownloadTem: true,//用于下载资源按钮状态控制
    cardLoadStatus: true,//加载翻牌时的状态，主要是用来控制按钮是否继续加载，true：可以继续加载
    searchLoadStatus: false,//是否处于搜索状态下的加载（防止正常的 下一页 加载冲突） true:是
    isLoadNotice: false,//是否加载过公告 true：已经加载过
    docScrollTop: 10,//文档垂直滚动距离
    doc_type: 1,//文档类型
    isScroll: false//防止自动滚动和手动滚动冲突
};
$(function () {
    /*$("html").niceScroll({
        cursorcolor:"#48c55e",
        cursoropacitymax:1,
        touchbehavior:false,
        cursorwidth:"3px",
        cursorborder:"0",
        cursorborderradius:"3px",
        autohidemode: false,
        mousescrollstep: 80
    });*/
    $(document).on('mousewheel', function () {//防止滚轮冲突
        var time = getUrlParm("t");//大分辨率会传递该参数
        var curTime = new Date().getTime();
        if (time && curTime - time <= 7 * 1000) {
            if (!globalVar.isScroll) {
                globalVar.isScroll = true;
                return false;
            } else {
                return true;
            }
        }
    })
})
$(function () {
    statistics();//统计信息
    resetScroll();//将文档滚动到合适位置
    loadNavBar();//加载导航条菜单
    navScrollEvent(768);//小于768分辨率导航条滚动监听
    loadFirstNotice();//加载第一条公告
    captionAnimate();//箭头动画
    scrollNoticeEvent();//公告滚动监听
    toTopEvent();//toTop监听
    $("#myName").click(blogNameClick);
    $("#myCarousel .carousel-caption > span").click(carouselClick);//轮播图向下按钮单机事件
    $(".nav").on({"mouseenter": navbarLiMouseenter, "mouseleave": navbarLiMouseleave,}, ".menu > li.dropdown");//导航条鼠标悬浮事件
    $("#search-input input").focus(searchInputFocus);//搜索框获取焦点事件
    $("#search-input input").blur(searchInputBlur);//搜索框失去焦点事件
    $("#search-input input").keydown(searchInputkeydown);//搜索框键盘监听事件
    $(".front-loading .loading_next").on("click", nextBtnClick);//左侧内容区域，加载内容
    $("#my-navbar .navbar-left").on("click", "a", navBarLeftClick);//左侧导航菜单单机事件
    $("#my-navbar .navbar-right").on("click", "a", navbarRightClick);//右侧导航菜单单机事件
    $("#zb-main-left").on("click", "a.card", cardClick);//翻牌点击事件
    $("#zb-main-left").on("click", "a.card .card-detail", detailClick);//翻牌点击事件
    $("#zb-main-right .notice a.more,#zb-main-right .notice .notice-cont").click(noticeMoreClick);//公告查看更多
});

function toTopEvent() {
    var top = 1500;
    var tem = true;
    var $top = $("a.toTop");
    $(document).on("scroll", function () {
        if ($(window).width() >= 1200) {
            if ($(document).scrollTop() >= top && tem) {
                tem = false;
                $top.show();
            } else if ($(document).scrollTop() < top) {
                tem = true;
                $top.hide();
            }
        } else {
            $top.hide();
        }
    });
    $top.click(function () {
        var mainLeftTop = $("#zb-main").offset().top;
        $("html,body").animate({scrollTop: (mainLeftTop - 150)}, 500);
    })

}

function statistics() {
    $.ajax({
        url: "/statistics.zb",
        type: "GET",
        dataType: "JSON"
    }).done(function (res) {
        if (res && res.success) {
            $(".artCount").text(res.data.artCount);
            $(".navbarCount").text(res.data.navbarCount);
            $(".commCount").text(res.data.commCount);
            $(".reqCount").text(res.data.reqCount);
            $(".praiseCount").text(res.data.praiseCount);
        }
    });
}

function resetScroll() {
    var time = getUrlParm("t");//大分辨率会传递该参数
    var curTime = new Date().getTime();
    if (time && curTime - time <= 7 * 1000) {
        var mainLeftTop = $("#zb-main").offset().top;
        $("html,body").animate({scrollTop: (mainLeftTop - 150)}, 500, function () {
            globalVar.isScroll = true;
        });
    }
}

function blogNameClick() {
    if ($(window).width() > 768) {
        window.location.href = "/?t=" + new Date().getTime()
    } else {
        window.location.href = "/";
    }
}

function carouselClick() {
    if ($(window).width() > 768) {
        var mainLeftTop = $("#zb-main").offset().top;
        $("html,body").animate({scrollTop: (mainLeftTop - 50)}, 1000)
    }
}

function captionAnimate() {
    var $carousel = $("#myCarousel .carousel-caption > span");
    $carousel.animate({top: -40}, 1000, "linear")
        .animate({top: -15}, 800, "linear")
        .animate({top: +15}, 800, "linear")
        .animate({top: -8}, 600, "linear")
        .animate({top: +12}, 400, "linear")
    setInterval(function () {
        $carousel.animate({top: -40}, 1000, "linear")
            .animate({top: -15}, 800, "linear")
            .animate({top: +15}, 800, "linear")
            .animate({top: -8}, 600, "linear")
            .animate({top: +12}, 400, "linear")
    }, 3600);
}

function nextBtnClick() {
    if (globalVar.cardLoadStatus) {
        $("#zb-main-left .front-loading .loading-next-div").hide();
        $("#zb-main-left .front-loading .loading_wait").show();
    }
    loadMainLeftContent();
}

function loadFirstNotice() {
    $.ajax({
        url: "/notice/getNoticeAll.zb",
        type: "POST",
        dataType: "JSON",
        data: {pageNum: globalVar.noticePageNum, pageSize: 1, enable: 1}
    }).done(function (data) {
        if (!data) {
            msg("加载公告失败！");
            return false;
        }
        var content = "";
        var noticeId = 0;
        if (data.size == 1) {
            noticeId = data.list[0].id;
            content = data.list[0].content;
            if (content && content.length > 60) {
                content = content.substring(0, 50) + ". . .";
            }
            $("#zb-main-right .notice a.more").show();
            var localNoticeId = localStorage.getItem("noticeId");
            if (localNoticeId != noticeId) {
                localStorage.setItem("noticeId", noticeId);
                noticeMoreClick();
            }
        }
        if (data.size == 0) {
            content = "暂 无 公 告 . . ."
        }
        $("#zb-main-right .notice .notice-cont").html(content);

    }).fail(function () {
        msg("可能由于网络原因，加载公告列表失败！");
    })
}

function loadNavBar() {
    var locNavBarJson = localStorage.getItem("navBarJson");
    var locNavBarJsonTime = localStorage.getItem("navBarJsonExpire");
    if (locNavBarJson && new Date().getTime() - locNavBarJsonTime <= 0) {
        loadNavBarDoneBack($.parseJSON(locNavBarJson));
        return false;
    }
    $.ajax({
        url: "/navbar/getNavBarByEnable.zb",
        type: "POST",
        dataType: "JSON"
    }).done(loadNavBarDoneBack).fail(function () {
        msg("加载导航菜单失败！");
    })
}

function loadNavBarDoneBack(data) {
    if (!data) {
        msg("加载导航菜单失败！");
        return false;
    }
    localStorage.setItem("navBarJson", JSON.stringify(data));
    localStorage.setItem("navBarJsonExpire", new Date().getTime() + globalVar.expireTime);
    var $ul_right = $("#my-navbar .aboutMeMenu");
    var $ul_left = $("#my-navbar ul.menu");
    for (var menu in data) {
        var menu = data[menu];
        var parentMenu = menu.parentMenu;
        var $li_left;
        if (menu.parentMenu.position == "left") {
            $li_left = $("<li class='dropdown'><a class='mainmenu' title='" + parentMenu.tip + "'><i class='" + parentMenu.icon_class + "'></i>" + parentMenu.menu_name + "</a></li>");
            $li_left.data("id", parentMenu.id);
            $li_left.data("url", parentMenu.menu_url);
            $li_left.data("isClick", parentMenu.is_click);
        } else if (menu.parentMenu.position == "right") {
            var $li_right = $("<li><a title='" + parentMenu.tip + "'><i class='" + parentMenu.icon_class + "'></i>" + parentMenu.menu_name + "</a></li>");
            $li_right.data("id", parentMenu.id);
            $li_right.data("url", parentMenu.menu_url);
            $li_right.data("isClick", parentMenu.is_click);
            $ul_right.append($li_right);
            continue;
        }
        var subMenu = menu.subMenu;
        if (subMenu && subMenu.length > 0) {
            var $subUl = $("<ul class='submenu'>")
            for (i = 0; i < subMenu.length; i++) {
                var $sub_li_left = $("<li><a  title='" + subMenu[i].tip + "'>" + subMenu[i].menu_name + "</a></li>");
                $sub_li_left.data("id", subMenu[i].id);
                $sub_li_left.data("url", subMenu[i].menu_url);
                $sub_li_left.data("isClick", subMenu[i].is_click);
                $subUl.append($sub_li_left)
            }
            $li_left.append($subUl)
        }
        $ul_left.append($li_left)
    }
}

function navbarLiMouseenter() {
    var winW = $(window).width();
    if (winW <= 768) {
        //防止在小分辨的时候出现ul.submenu重叠现象
        $(this).siblings("li").find("ul.submenu").css({"visibility": "hidden"});
    }
    //1.让当前ul.submenu可见
    //2.如果从小分辨率进行鼠标悬浮事件之后再换到大分辨率窗口可能出现部分ul.submenu处于影藏状态
    $(this).find("ul.submenu").css("visibility", "visible");
    var $sub_lis = $(this).find("ul.submenu > li");
    $sub_lis.each(function (index, elem) {
        $sub_lis.eq(index).css({
            "transform": "rotateY(0deg)",
            "transition-delay": index * 100 + "ms"
        })
    })
    $(".menu").css("z-index", 2);
}

function navbarLiMouseleave() {
    $(this).find("ul.submenu").css("z-index", "1")
    var $sub_lis = $(this).find(".submenu li");
    $sub_lis.each(function (index, elem) {
        $sub_lis.eq(index).css({
            "transform": "rotateY(90deg)",
            "transition-delay": ($sub_lis.length - 1 - index) * 100 + "ms"
        })
    })
    $(".menu").css("z-index", 0);
}

function searchInputFocus() {
    $(this).siblings("span").css({
        "transform": "rotateX(0deg)",
        "transition": "all 0.5s"
    });
}

function searchInputkeydown() {
    var code = event.keyCode;
    if (code == 13) {
        if ($(window).width() <= 768) {
            $("#my-navbar").removeClass("in").attr("aria-expanded", false);
        } else {
            var mainLeftTop = $("#zb-main").offset().top;
            $("html,body").animate({scrollTop: (mainLeftTop - 150)}, 500);
        }
        showShade();
        $("#zb-main-left .zb-content").empty();
        globalVar.pageNum = 1;//重置分页
        globalVar.searchLoadStatus = true;//置为搜索状态
        globalVar.cardLoadStatus = true;//释放加载card项
        globalVar.navBarId = "";
        loadMainLeftContent();
        return false;
    }
}

function searchInputBlur() {
    $(this).siblings("span").css({
        "transform": "rotateX(90deg)",
        "transition": "all 0.5s"
    })
}

function scrollNoticeEvent() {
    if ($(".notice").is(":visible")) {
        $(document).on("scroll", function () {
            var scrollTop = $(this).scrollTop();
            var mainLeftTop = $(".friendLink").offset().top + $(".friendLink").height() + 10;
            if (scrollTop >= mainLeftTop) {
                $(".notice").css({"position": "fixed", "top": 60});
            } else {
                $(".notice").css({"position": "relative", "top": 0});
            }
        });
    }
}

function cardClick() {
    if ($(window).width() >= 768) {
        var artId = $(this).attr("data-id");
        toDetailPage(artId)
    }
}

function detailClick() {
    var artId = $(this).parents("a.card").attr("data-id");
    toDetailPage(artId)
}

function toDetailPage(artId) {
    window.open("/frontstage/detail.html?id=" + artId + "&t=" + new Date().getTime());
}

function navBarLeftClick() {
    var $li = $(this).parent("li");
    var isClick = $li.data("isClick");
    if (isClick) {
        if ($(window).width() <= 768) {
            $("#my-navbar").removeClass("in").attr("aria-expanded", false);
        } else {
            var mainLeftTop = $("#zb-main").offset().top;
            $("html,body").animate({scrollTop: (mainLeftTop - 150)}, 500);
        }
        showShade();
        var id = $li.data("id");
        globalVar.navBarId = id;
        globalVar.pageNum = 1;
        globalVar.cardLoadStatus = true;//释放加载按钮
        $("#zb-main-left .zb-content").empty();
        loadMainLeftContent();
    }
}

function navbarRightClick() {
    var url = $(this).parent("li").data("url");
    if (url) {
        window.location.href = url + "?t=" + new Date().getTime();
    }
}

function loadMainLeftContent() {
    if (!globalVar.cardLoadStatus) {
        return false;
    }
    globalVar.cardLoadStatus = false;//阻止连续点击事件进入
    $(".front-loading .loading_next").text("加载更多 。。。");//防止按钮被制为最后一页内容或空数据内容
    if ($(".front-loading").is(":hidden")) {//这里主要要是为了解决详情页面点击菜单展示翻牌loading可能不显示的问题
        $(".front-loading").show();
    }
    //$("#zb-main-left .front-loading .loading-next-div").hide();
    //$("#zb-main-left .front-loading .loading_wait").show();
    var def = $.ajax({
        url: "/article/getArticleByStatus.zb",
        type: "POST",
        dataType: "JSON",
        data: {
            pageNum: globalVar.pageNum,
            pageSize: globalVar.pageSize,
            navBarId: globalVar.navBarId,
            keyWord: $("#search-input input").val().trim()
        }
    });
    def.done(function (data) {
        if (data && data.list && data.list.length > 0) {
            for (var i = 0; i < data.list.length; i++) {
                var json = data.list[i];
                var content = "<div class='col-lg-4 col-md-4 col-sm-6 zb-cnt-card'>" +
                    "	<a target='_blank' class='card' data-id='" + json.id + "'>" +
                    "		<div class='thumbnail'>" +
                    "			<div class='front'>" +
                    "				<img src='" + json.front_img + "'>" +
                    "				<div class='caption'>" +
                    "					<h4>" + json.title + "</h4>" +
                    "					<div class='card-desc'>" + json.front_descr + "</div>" +
                    "				</div>" +
                    "                <div class='card-detail visible-xs'> 查&nbsp;看&nbsp;详&nbsp;情 . . .</div>" +
                    "			</div>" +
                    "			<div class='back'>" +
                    "				<div class='caption'>" +
                    "					<h3>" + json.title + "</h3>" +
                    "					<header>" +
                    "						<p>作者：<span>" + json.user_name + "</span></p>" +
                    "						<p>时间：<span>" + json.create_time + "</span></p>" +
                    "						<p>分类：<span>" + json.blog_navbar_name + "</span></p>" +
                    "						<p>人气：<span>" + json.visit_count + "</span></p>" +
                    "					</header>" +
                    "					<div class='card-desc'>" + json.back_descr + "</div>" +
                    "                   <div class='card-detail visible-xs'> 查&nbsp;看&nbsp;详&nbsp;情 . . .</div>" +
                    "				</div>" +
                    "			</div>" +
                    "		</div>" +
                    "	</a>" +
                    "</div>";

                $("#zb-main-left .zb-content").append($(content));
            }
            globalVar.pageNum++;
            globalVar.cardLoadStatus = true;//释放加载事件
            if (data.isLastPage) {
                //$(".front-loading .loading_next").off();
                globalVar.cardLoadStatus = false;
                $(".front-loading .loading_next").text("我是有底线的! (^_^)");
            }
        } else if (data && data.list && data.list.length == 0) {
            //$(".front-loading .loading_next").off();
            //globalVar.cardLoadStatus = false;//禁止点击
            globalVar.cardLoadStatus = false;
            $(".front-loading .loading_next").text("空空如也! ⊙﹏⊙");
        } else {
            globalVar.cardLoadStatus = true;//释放加载事件
            msg("加载内容失败！");
        }
    })
    def.fail(function () {
        globalVar.cardLoadStatus = true;//释放加载事件
        msg("加载内容失败！");
    })
    def.always(function () {
        hideShade();
        globalVar.searchLoadStatus = false;//搜做完毕，置为非搜索状态
        //globalVar.navBarId = "";
        $("#zb-main-left .loading_wait").hide();
        $("#zb-main-left .loading-next-div").show();
    })
}

function noticeMoreClick() {
    var $fixedNotice = $(".fixedNotice");
    $fixedNotice.click(function (e) {
        if (!$(e.target.offsetParent).hasClass("panel-notice")) {
            $(this).fadeOut();
            $(this).find(".panel-notice").slideUp();
            $fixedNotice.off();
        }
    })
    if (globalVar.isLoadNotice) {
        $fixedNotice.fadeIn();
        $fixedNotice.find(".panel-notice").slideDown();
    } else {
        $fixedNotice.load("/page/notice/front/index.html?t=" + new Date().getTime(), function () {
            $(this).fadeIn();
            $(this).find(".panel-notice").slideDown();
            globalVar.isLoadNotice = true;
        })
    }
}

function navScrollEvent(pixel) {
    if ($(window).width() <= pixel) {
        $(document).scroll(function () {
            var docScrollTop = $(this).scrollTop();
            if (docScrollTop < globalVar.docScrollTop) {
                //navBarShow();
                globalVar.docScrollTop = docScrollTop;
            } else {
                //navBarHide();
                globalVar.docScrollTop = docScrollTop;
            }
        });
    }
}

function navBarShow() {
    var $nav = $(".nav.navbar-static-top");
    if ($nav.is(":hidden")) {
        $nav.slideDown();
    }
}

function navBarHide() {
    var $nav = $(".nav.navbar-static-top");
    if ($nav.is(":visible")) {
        $nav.slideUp();
    }
}

function msg(msg) {
    if (!msg) {
        msg = "";
    }
    if ($(window).width() >= 992) {
        layer.msg(msg, {offset: [$(window).height() / 2 - 100, $("#zb-main-left").width() / 2 - (msg.length * 12 / 2)]}, fn)
    } else {
        layer.msg(msg, fn)
    }

}

function getUrlParm(param) {
    var reg = new RegExp("(^|&)" + param + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null)
        return unescape(r[2]);
    return null;
}

function fn() {
}

console.log("%c对偷窥者造成10000点暴击！", "background: rgba(252,234,187,1);background: -moz-linear-gradient(left, rgba(252,234,187,1) 0%, rgba(175,250,77,1) 12%, rgba(0,247,49,1) 28%, rgba(0,210,247,1) 39%,rgba(0,189,247,1) 51%, rgba(133,108,217,1) 64%, rgba(177,0,247,1) 78%, rgba(247,0,189,1) 87%, rgba(245,22,52,1) 100%);background: -webkit-gradient(left top, right top, color-stop(0%, rgba(252,234,187,1)), color-stop(12%, rgba(175,250,77,1)), color-stop(28%, rgba(0,247,49,1)), color-stop(39%, rgba(0,210,247,1)), color-stop(51%, rgba(0,189,247,1)), color-stop(64%, rgba(133,108,217,1)), color-stop(78%, rgba(177,0,247,1)), color-stop(87%, rgba(247,0,189,1)), color-stop(100%, rgba(245,22,52,1)));background: -webkit-linear-gradient(left, rgba(252,234,187,1) 0%, rgba(175,250,77,1) 12%, rgba(0,247,49,1) 28%, rgba(0,210,247,1) 39%, rgba(0,189,247,1) 51%, rgba(133,108,217,1) 64%, rgba(177,0,247,1) 78%, rgba(247,0,189,1) 87%, rgba(245,22,52,1) 100%);background: -o-linear-gradient(left, rgba(252,234,187,1) 0%, rgba(175,250,77,1) 12%, rgba(0,247,49,1) 28%, rgba(0,210,247,1) 39%, rgba(0,189,247,1) 51%, rgba(133,108,217,1) 64%, rgba(177,0,247,1) 78%, rgba(247,0,189,1) 87%, rgba(245,22,52,1) 100%);background: -ms-linear-gradient(left, rgba(252,234,187,1) 0%, rgba(175,250,77,1) 12%, rgba(0,247,49,1) 28%, rgba(0,210,247,1) 39%, rgba(0,189,247,1) 51%, rgba(133,108,217,1) 64%, rgba(177,0,247,1) 78%, rgba(247,0,189,1) 87%, rgba(245,22,52,1) 100%);background: linear-gradient(to right, rgba(252,234,187,1) 0%, rgba(175,250,77,1) 12%, rgba(0,247,49,1) 28%, rgba(0,210,247,1) 39%, rgba(0,189,247,1) 51%, rgba(133,108,217,1) 64%, rgba(177,0,247,1) 78%, rgba(247,0,189,1) 87%, rgba(245,22,52,1) 100%);filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#fceabb', endColorstr='#f51634', GradientType=1 );font-size:5em;")
console.log("%c", "padding:50px 300px;line-height:120px;background:url('http://home.zhangbin.xin/static/img/face/comment/33.gif') repeat;")